package mongo

import (
	"context"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"time"
)

type ChatUser struct {
	UserId uint   `bson:"user_id"`
	Name   string `bson:"name"`
}

type Chat struct {
	ChatId    string     `bson:"_id"`
	Owner     uint       `bson:"owner"`
	Users     []ChatUser `bson:"users"`
	ChatName  string     `bson:"chat_name"`
	CreatedAt time.Time
}

func (c Chat) CollectionName() string {
	return "chat"
}

func (c Chat) Init(mdb *mongo.Database) error {
	collection := mdb.Collection(c.CollectionName())

	indexOptions := options.Index().SetUnique(false)
	indexModel := mongo.IndexModel{
		Keys:    bson.D{{Key: "owner", Value: 1}, {Key: "created_at", Value: -1}},
		Options: indexOptions,
	}

	_, err := collection.Indexes().CreateOne(context.TODO(), indexModel)
	if err != nil {
		return err
	}

	return nil
}

type GetChatsParam struct {
	UserId uint
	Skip   int64
	Limit  int64
	Order  map[string]int
}
